package mq

import (
	"context"
	"time"

	"github.com/lab-online/internal/lab/entity"
	"github.com/lab-online/pkg/mq"
)

// ProduceExperiment 发布执行实验消息
func (m *MQ) ProduceExperiment(labEntity entity.LabEntity) (entity.LabEntity, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	if err := m.PublishWithContext(ctx,
		mq.QueueDeclareConfig{
			Name:    labEntity.GetRouterKey(),
			Durable: true,
		},
		mq.Publishing{
			Message: mq.Message{
				Body:            labEntity.GetEncodedArguments(),
				ContentType:     "application/json",
				ContentEncoding: "utf-8",
			},
			RouterKey: labEntity.GetRouterKey(),
		},
	); err != nil {
		return nil, err
	}

	return labEntity, nil
}
